package edu.msu.scrabble.project1;

import com.google.android.gcm.GCMBaseIntentService;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

public class GCMIntentService extends GCMBaseIntentService {

	public static final String SENDER_ID = "635127309086";
	
	private Cloud thecloud = Cloud.get();
	
	public GCMIntentService() {
		super(SENDER_ID);
		// TODO Auto-generated constructor stub
	}
	
	public GCMIntentService(String... senderIds){
		super(senderIds);
	}

	@Override
	protected void onError(Context context, String errorId) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onMessage(Context context, Intent message) {
		String msg = message.getStringExtra("message");
		
		if(msg.equals("operation")) {
			Intent intent = new Intent(this, MainActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
			builder.setSmallIcon(R.drawable.ic_launcher);
			builder.setContentTitle(context.getString(R.string.app_name));
			builder.setContentText(context.getString(R.string.notify));
			builder.setAutoCancel(true);
			builder.setContentIntent(pendingIntent);
			
			NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			mNotificationManager.notify(0, builder.build());
		}
		
		else if(msg.equals("startgame")) {
			//Get info for the game!
			if(thecloud.getGameInfoFromServer()) {
//				 If we are player1 go to edit
//				 If there is enough time this should be changed to a notification service
				NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
				builder.setSmallIcon(R.drawable.ic_launcher);
				builder.setContentTitle(context.getString(R.string.app_name));
				builder.setContentText(context.getString(R.string.join_game));
				builder.setTicker(context.getString(R.string.join_game));
				builder.setAutoCancel(true);
				PendingIntent pendingIntent;
				
				thecloud.setNotLeavingApp(true);
				
				NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				
				if(thecloud.getUsername().equals(thecloud.getGameInfoTurn())) {
//					Toast.makeText(this, R.string.starting_game, Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(this, EditActivity.class);
//					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
				}
				// If we are player2 go to waiting
				else {
//					Toast.makeText(this, R.string.starting_game, Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(this, WaitingActivity.class);
//					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
				}
				builder.setContentIntent(pendingIntent);
				Notification notification = builder.build();
				notification.flags |= Notification.FLAG_NO_CLEAR;
				mNotificationManager.notify(0, notification);
			}
			// We failed to get the info for the game
			else {
				// DECIDE WHAT TO DO HERE, like try again, not just log out
				Toast.makeText(this, "start game fail", Toast.LENGTH_SHORT).show();
				thecloud.logOut();
			}
		}
		else if(msg.equals("updategame")) {
			// Got the game info
			String previousTurn = thecloud.getGameInfoTurn();
			
			if(thecloud.getGameInfoFromServer() && thecloud.getGameDataFromServer()) {
				
				thecloud.setNotLeavingApp(true);
				// It is our turn to be active
				if(thecloud.getGameInfoTurn().equals(thecloud.getUsername())){
					// We are drawing
					/**
					if(thecloud.getGameInfoSetting().equals("drawing")) {
						Intent intent = new Intent(this, EditActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getApplication().startActivity(intent);
					}
					**/
					// We are guessing
					//else 
					if(thecloud.getGameInfoSetting().equals("guessing")) {
						Intent intent = new Intent(this, GuessActivity.class);
//						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getApplication().startActivity(intent);
					}
					
					// Something went horribly wrong :'(
					else {
						thecloud.logOut();
					}
				}
				// We need to wait
				else {
					//need to make it clear whether or not we are already in waiting
					// if player2 had the previous turn then we are staying in waiting
					if(previousTurn.equals(thecloud.getGameInfoPlayer2Name())) {
						// Do nothing
					}
					else {
						Intent intent = new Intent(this, WaitingActivity.class);
//						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getApplication().startActivity(intent);
					}
				}
			}
			
			
			
			// Failed to get the game info
			else {
				// DECIDE WHAT TO DO HERE, like try again, not just log out
				Toast.makeText(this, "update game fail", Toast.LENGTH_SHORT).show();
				thecloud.logOut();
			}
		}
		
		
		else if (msg.equals("gameover")) {
			
			if (thecloud.getGameInfoFromServer()) {
				thecloud.setEndGameFlag(true);
				
				Intent intent = new Intent(this, FinalActivity.class);
//				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getApplication().startActivity(intent);
			}
			
			// Something went horribly wrong :'(
			else {
				Toast.makeText(this, "game over fail", Toast.LENGTH_SHORT).show();
				thecloud.logOut();
			}
			
		}
		
		else if (msg.equals("playerleft")) {
			thecloud.setEndGameFlag(true);
			
			Intent intent = new Intent(this, FinalActivity.class);
//			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getApplication().startActivity(intent);
		}
	}

	@Override
	protected void onRegistered(Context context, String regId) {
		thecloud.setGcmId(regId);
		
	}

	@Override
	protected void onUnregistered(Context context, String regId) {
		// TODO Auto-generated method stub
		
	}
}
